<%@ page pageEncoding="UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<script src="/static/js/jquery.min.js"></script>
<c:set var="p" value="${sessionScope.pagination}"/>
<c:choose>
    <c:when test="${p.totalRows eq 0}">
        <em>没有记录</em>
    </c:when>
    <c:otherwise>
        <c:choose>
            <c:when test="${p.currentPage eq 1}">
                首页
                上一页
            </c:when>
            <c:otherwise>
                <a href="/${param.path}/1">首页</a>
                <a href="/${param.path}/${p.currentPage - 1}">上一页</a>
            </c:otherwise>
        </c:choose>
        <c:if test="${p.currentPage < 7}">
            <c:if test="${p.totalPages <= 7}">
                <c:forEach var="page" begin="1" end="${p.totalPages}">
                    <a href="/${param.path}/${page}">${page}</a>
                </c:forEach>
            </c:if>
            <c:if test="${p.totalPages > 7}">
                <c:forEach var="page" begin="1" end="7">
                    <a href="/${param.path}/${page}">${page}</a>
                </c:forEach>
                ...
            </c:if>
        </c:if>
        <c:if test="${p.currentPage >= 7}">
            <a href="/${param.path}/1">1</a>
            <a href="/${param.path}/2">2</a>
            ...
            <a href="/${param.path}/${p.currentPage-2}">${p.currentPage-2}</a>
            <a href="/${param.path}/${p.currentPage-1}">${p.currentPage-1}</a>
            <a href="/${param.path}/${p.currentPage}">${p.currentPage}</a>
            <c:if test="${p.totalPages - p.currentPage > 2}">
                <a href="/${param.path}/${p.currentPage+1}">${p.currentPage+1}</a>
                <a href="/${param.path}/${p.currentPage+2}">${p.currentPage+2}</a>
                ...
            </c:if>
            <c:if test="${p.totalPages - p.currentPage <= 2}">
                <c:forEach var="page" begin="${p.currentPage+1}"
                           end="${p.totalPages}">
                    <a href="/${param.path}/${page}">${page}</a>
                </c:forEach>
            </c:if>
        </c:if>
        <c:choose>
            <c:when test="${p.currentPage eq p.totalPages}">
                下一页
                尾页
            </c:when>
            <c:otherwise>
                <a href="/${param.path}/${p.currentPage + 1}">下一页</a>
                <a href="/${param.path}/${p.totalPages}">尾页</a>
            </c:otherwise>
        </c:choose>
        共 ${p.totalPages} 页
        <form style="display: inline-block">
            到第
            <select id="page">
                <c:forEach var="i" begin="1" end="${p.totalPages}">
                    <option value="${i}">${i}</option>
                </c:forEach>
            </select>
            页
            <a id="submit" href="#">确定</a>
        </form>
    </c:otherwise>
</c:choose>
<script>
    $(function () {
        $('#submit').on('click', function () {
            $('form').attr('action', '/${param.path}/' + $('select').val()).trigger('submit');
        });
        $('option[value=' + ${p.currentPage} +']').attr('selected', 'selected');
        $('a:contains(' + ${p.currentPage} +')').css('color', 'red');
    });
</script>